/**
 * Created by lyf on 2017/6/20.
 */

'use strict'
let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = async function deployFluentd (elasticsearch , accesspoint, kong , seckillManager, orderhandle){
    let fluentd = [];
    for (let i = 1 ; i < arguments.length; i++){
        if (arguments[i]){
            fluentd = fluentd.concat(arguments[i]);
        }

    }

    return new Promise (async function(resolve,reject){
        console.log(fluentd);
        for ( let i = 0 ; i < fluentd.length ; i++){
            await ssh.connect({
                host: fluentd[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            // `docker run -d  --restart=on-failure:5 --privileged   -p 24224:24224 -p 24224:24224/udp ` + 
            // ` --name fluentd-mongodb-node ` +
            // ` --env mongodb_host=dds-2ze6e67059a0f1a41.mongodb.rds.aliyuncs.com ` +
            // ` --env mongodb_port=3717 `+
            // ` --env mongodb_database=admin `+ 
            // ` --env mongodb_collection=node${i} `+
            // ` --env mongodb_user=root `+
            // ` --env mongodb_password=Infinitus2016 `+ 
            // ` foreveross/fluentd-mongodb `

// ` docker run -d  --restart=on-failure:5` +
//                   ` --privileged  --name fluentd ` +
//                   ` -p 24224:24224 -p 24224:24224/udp  ` +
//                   ` -v /root/seckill/fluentd/fluent.conf:/fluentd/etc/fluent.conf ` +
//                   ` -v /root/seckill/fluentd/:/fluentd/log/ ` +
//                   ` foreveross/fluentd-kafka `

            //   ` docker run -d  --restart=on-failure:5` +
            //       ` --privileged  --name fluentd ` +
            //       ` -p 24224:24224 -p 24224:24224/udp  ` +
            //       ` -v /root/seckill/fluentd/fluent.conf:/fluentd/etc/fluent.conf ` +
            //       ` foreveross/fluentd-es `
            
            const result = await ssh.execCommand(` docker run -d  --restart=on-failure:5` +
                  ` --privileged  --name fluentd ` +
                  ` -p 24224:24224 -p 24224:24224/udp  ` +
                  ` -v /root/seckill/fluentd/fluent.conf:/fluentd/etc/fluent.conf ` +
                  ` -v /root/seckill/fluentd/:/fluentd/log/ ` +
                  ` foreveross/fluentd-all `);
            ssh.dispose();
            //result.stdout ? resolve() : reject ();

            console.log(`fluentd:${fluentd[i][1]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            resolve();

        }
    })

};
